package com.lzh.financial.controller;

import ch.qos.logback.core.util.TimeUtil;
import cn.hutool.core.date.Month;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.financial.bean.PUserMoneyBean;
import com.lzh.financial.bean.PpayBean;
import com.lzh.financial.params.MoneyParam;
import com.lzh.financial.params.PageParam;
import com.lzh.financial.params.PageVo;
import com.lzh.financial.service.IPayService;
import com.lzh.financial.service.IuserMoneyService;
import com.lzh.financial.util.JwtTokenUtil;
import com.lzh.financial.util.PayMoneyUtil;
import com.lzh.financial.util.RebornJson;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/money")
public class UserMoneyController {

    @Resource
    private IuserMoneyService userMoneyService;
    @Resource
    private IPayService payService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 获取全部金额的金额
     * @return
     */
    @RequestMapping(value = "/getTotalMoney",method = RequestMethod.GET)
    public JSONObject getTotalMoney(PageVo pageVo,Integer userId,Integer way,Integer order,HttpSession session){
        PageHelper.startPage(pageVo.getPageNo(),pageVo.getPageSize());
        if (ObjectUtil.isNull(userId)||userId==0){
            userId = Integer.valueOf(jwtTokenUtil.getUserIdFromToken(session));
        }
        List<PpayBean> moneyByDate = payService.getMoneyAll(userId, way,order);
        PageInfo<PpayBean> pageInfo = new PageInfo<>(moneyByDate);
        return RebornJson.successList("返回成功",pageInfo.getList(),new PageParam(pageInfo));
    }

    /**
     * 资金统计 echart
     * @return
     */
    @RequestMapping(value = "/getMoneyByDate",method = RequestMethod.GET)
    public JSONObject getMoneyByDate(Integer userId,HttpSession session){
        if (ObjectUtil.isNull(userId)||userId==0){
            userId = Integer.valueOf(jwtTokenUtil.getUserIdFromToken(session));
        }
        List<PpayBean> moneyByDate1 = payService.getMoneyByDate(userId, 1);
        List<PpayBean> moneyByDate2 = payService.getMoneyByDate(userId, 2);
        HashMap<String, Map> map = new HashMap<>();
        Map map1 = PayMoneyUtil.setStatistcs(moneyByDate1);
        Map  map2= PayMoneyUtil.setStatistcs(moneyByDate2);
        Map sum = PayMoneyUtil.getSums(map2, map1);
        map.put("z", PayMoneyUtil.mapConverList(map2));
        map.put("f", PayMoneyUtil.mapConverList(map1));
        map.put("sum",PayMoneyUtil.mapConverList(sum) );
        return RebornJson.success("返回成功",map);
    }

    /**
     * 获取全部金额的消费排行
     * @return
     */
    @RequestMapping(value = "/getOrderType",method = RequestMethod.GET)
    public JSONObject getOrderType(Integer userId,Integer pmonth, Integer pyear,HttpSession session){
        if (ObjectUtil.isNull(userId)||userId==0){
            userId = Integer.valueOf(jwtTokenUtil.getUserIdFromToken(session));
        }
        List<PpayBean> orderType1 = payService.getOrderType(userId, 2,pmonth,pyear);
        List<PpayBean> orderType2 = payService.getOrderType(userId, 1,pmonth,pyear);
        HashMap<String, List<PpayBean>> map = new HashMap<>();
        map.put("get",orderType1);
        map.put("pay",orderType2 );
        return RebornJson.success("返回成功",map);
    }


    /**
     * 获取自己当前的金额
     * @return
     */
    @RequestMapping(value = "/getMyMoney",method = RequestMethod.GET)
    public JSONObject getMyMoney(HttpSession session){
        PUserMoneyBean money = userMoneyService.getTotalMoneyById(jwtTokenUtil.getUserIdFromToken(session));
        return RebornJson.success("总额",money);
    }
}
